package com.freesky.quartz.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.freesky.biz.rights.domain.RightsPayEntity;
import com.freesky.biz.rights.domain.RightsPayIcbcEntity;
import com.freesky.biz.rights.domain.RightsPayRefundEntity;
import com.freesky.biz.rights.domain.RightsReceiveEntity;
import com.freesky.biz.rights.mapper.RightsActivityMapper;
import com.freesky.biz.rights.mapper.RightsBusiMapper;
import com.freesky.biz.rights.mapper.RightsPayMapper;
import com.freesky.biz.rights.mapper.RightsPayRefundMapper;
import com.freesky.biz.rights.service.IRightsPayIcbcService;
import com.freesky.biz.rights.service.IRightsReceiveService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.constant.CommonConstants;
import com.freesky.common.constant.LotteryConstants;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.redis.RedisCache;
import com.freesky.framework.web.service.SendAlarmService;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.service.WxPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 工行爱购8.8元支付超时，券数回退
 */
@Component("rightsPayIcbcTask")
public class RightsPayIcbcTask {

    private static final Logger logger = LoggerFactory.getLogger(RightsPayIcbcTask.class);

    @Autowired
    private SendAlarmService sendAlarmService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IRightsPayIcbcService rightsPayIcbcService;

    @Autowired
    private IRightsReceiveService rightsReceiveService;

    @Autowired
    private RightsBusiMapper rightsBusiMapper;

    @Autowired
    private RightsActivityMapper rightsActivityMapper;
    public void taskProcess() {
        logger.info("rightsPayIcbcTask task start");
        String startDt = DateUtil.format(DateUtil.offsetMinute(new Date(),-30), "yyyy-MM-dd HH:mm:ss");
        System.out.println("startDt:" + startDt);

        /**
         * 工行爱购惊喜8.8元
         */
        List<String> busiIdList = new ArrayList<>();
        busiIdList.add("ogp9El7D");
        List<RightsPayIcbcEntity> rightsPayList = rightsPayIcbcService.selectUnProcessed(busiIdList,startDt);
        for(RightsPayIcbcEntity rightsPayIcbc : rightsPayList){
            String lotteryKey = StrUtil.format(LotteryConstants.LOTTERY, rightsPayIcbc.getBusiId());
            String lotteryPrizeRedisKey = StrUtil.format(LotteryConstants.LOTTERY_PRIZE,
                    rightsPayIcbc.getBusiId(),
                    rightsPayIcbc.getActivityId());
            long validStock = redisCache.increment(lotteryPrizeRedisKey, "leftCoupons", 1);
            //更新总数
            redisCache.increment(lotteryKey, "leftCoupons", 1);

            RightsPayIcbcEntity updateRightsPayIcbc = new RightsPayIcbcEntity();
            updateRightsPayIcbc.setId(rightsPayIcbc.getId());
            updateRightsPayIcbc.setProcessed("1");
            updateRightsPayIcbc.setUpdateTime(new Date());
            rightsPayIcbcService.updateById(updateRightsPayIcbc);
        }

        busiIdList.clear();
        //福建工行e生活M5挑战赛
        busiIdList.add("OAwyLwBy");
        //爱购达标“油”礼
        busiIdList.add("qjZ5QBl8");
        //爱购周期五
        busiIdList.add("qd9R5Yxx");

        List<RightsReceiveEntity> rightsReceiveList = rightsReceiveService.selectUnPayList(busiIdList,startDt);
        for(RightsReceiveEntity rightsReceive : rightsReceiveList){
            //回退券数
            String lotteryKey = StrUtil.format(LotteryConstants.LOTTERY, rightsReceive.getBusiId());
            String lotteryPrizeRedisKey = StrUtil.format(LotteryConstants.LOTTERY_PRIZE,
                    rightsReceive.getBusiId(),
                    rightsReceive.getActivityId());
            long validStock = redisCache.increment(lotteryPrizeRedisKey, "leftCoupons", 1);
            redisCache.increment(lotteryKey, "leftCoupons", 1);

            rightsReceiveService.updateSendFlagById(rightsReceive.getId(),"4");

            //更新数据库券数
            rightsBusiMapper.updateBusiCount(rightsReceive.getBusiId(),-1);

            rightsActivityMapper.updateRightsActivityCount(rightsReceive.getActivityId(),-1);
        }
    }
}
